<?php
/**
* Django's templating engine (partial) PHP implementation.
* provides some template tags support
* @link http://djangoproject.com
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author P.Buth <neakdo@gmail.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* provides sum tag
* syntax : {% sum value1 value2 %}
*/
class DjTplTagNode_sum extends DjTplTagNode {


    /**
    * builds itself from tokens
    * @param djangoTplToken &$selfToken
    * @param array &$nextTokens djangoTplToken
    * @access public
    * @return bool
    */
    function buildFromTokens(&$selfToken) {
        $def=$selfToken->tagdef;

        $this->operands = explode(' ', $def);

        return true;
    }

    /**
    * builds itself from tokens
    * @param djangoTplToken &$selfToken
    * @param array &$nextTokens djangoTplToken
    * @access public
    * @return bool
    */
    function isAtomic() {
        return true;
    }

    /**
    * renders as string
    * @param djangoTplContext $context
    * @access public
    * @return string
    */
    function render(&$context) {

        $sum = 0;

        foreach($this->operands as $operand) {

            $value = djtplParser::evalExpression($context, $operand);

            $sum += $value !== NULL ? $value : 0 ;

        }

        return $sum;

    }
}
